//
//  ZhuShouTwoVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/9/12.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "ZhuShouTwoVC.h"
#import "RKTagsMenuView.h"
#import "TodoHttpManager.h"
#import "ZhuShouThreeVC.h"
#import "ZhuTwoCell.h"

#import "MJExtension.h"
#import "HouseModel.h"
#import "LXAlertView.h"
@interface ZhuShouTwoVC ()<UITableViewDelegate,UITableViewDataSource,ZhuTwoCellDelegate>

{
    NSString *_unitStr;
    NSMutableDictionary *_zShouDic;
    NSMutableArray *_zonArr;
    NSString *houseID;
    NSString *_colorStr;//状态,-----颜色
}


@property(nonatomic,strong)UIView *aView;
@property(nonatomic,strong)NSMutableArray *zhuArr;
@property (nonatomic,strong) NSMutableArray *qiArr;
@property(nonatomic,strong)UITableView *tabView;

@property (nonatomic,strong) NSMutableArray *oneArr;

@property (nonatomic,strong) NSMutableArray *twoArr;

@property (nonatomic,strong) NSMutableArray *threeArr;
@property(nonatomic,strong)HouseModel *houModel;
@property (nonatomic,strong) NSMutableArray *floorArr;
@property(nonatomic,strong)NSMutableArray *oModelArr;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *changArr;
@property(nonatomic,strong)NSMutableArray *lastArr;



@end

@implementation ZhuShouTwoVC

-(void)viewDidAppear:(BOOL)animated
{
    //主售房源(单元)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoCeZhuUnit:) name:TDCeoCeZhuUnit object:nil];
    if (_zhuArr.count == 0)
    {
        [_manager setUpCeoShenPiZhuShouUnitWithProjectID:_projectID];

    }
  
    //主售房源(房源)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoCezhuHOuse:) name:TDCeoCezhuHOuse object:nil];
    
    //优惠政策执行
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didZhuAgreeAndUnAgree:) name:TDCeoCeAgree object:nil];
    //优惠政策驳回
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didZhuAgreeAndUnAgree:) name:TDCeoCeUNAgree object:nil];
}
// 主售房源(单元)
-(void)didTDCeoCeZhuUnit:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            
            _zhuArr = [notifi.object objectForKey:@"data"];
            
            _zShouDic = [_zhuArr objectAtIndex:0];
            _unitStr = [_zShouDic objectForKey:@"unit"];
            NSLog(@"-----6-------%@---",_unitStr);
            for (int i = 0; i < _zhuArr.count; i++) {
                
                NSDictionary *dict = [_zhuArr objectAtIndex:i];
                [_qiArr addObject:dict[@"name"]];
                
            }
            
            [_tabView reloadData];
            [self setupMenuQiArr:_qiArr];
            [super viewDidLoad];
            
            
        }
        else
        {
#pragma mark----------------失败的加载
            [_manager MakeMyselfAlert:@"正在加载" andIsNeedTitle:NO andShowTime:5.0];
        }
    }
    

}

 //主售房源(房源)
-(void)didTDCeoCezhuHOuse:(NSNotification *)noti
{
    if (noti.object !=0)
    {
        NSLog(@"notifi.object===%@",noti.object);
        self.houModel = [HouseModel mj_objectWithKeyValues:noti.object];
        
      
        for (DataModel *dataA in self.houModel.data)
        {
            
            [self.floorArr addObject:dataA.floor];
            NSLog(@"-------%@",dataA.floor);
            
            [self.dataArr addObject:dataA.data];
            
            
            
            for (DataModel2 *dataB in dataA.data)
            {
                
                [self.oModelArr addObject:dataB];
                
            }
            
            
        }
        
        
        
        
        [self.tabView reloadData];
    }

    
    
    
}
//优惠政策的执行OR驳回
-(void)didZhuAgreeAndUnAgree:(NSNotification *)not
{
    if (not.object !=0 )
    {
        NSString *status = [not.object objectForKey:@"status"];
        NSString *info = [not.object objectForKey:@"info"];
        if ([status isEqualToString:@"1"])
        {
            [self.navigationController popViewControllerAnimated:NO];
        }
        else
        {
            [_manager MakeMyselfAlert:info andIsNeedTitle:NO andShowTime:2];
        }
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeZhuUnit object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCezhuHOuse object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeBusness object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeAgree object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeUNAgree object:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠政策审批";
    self.view.backgroundColor = BgColor;
    
    _manager = [TodoHttpManager sharedManager];
    
    _zhuArr = [[NSMutableArray alloc] init];
    _zShouDic = [[NSMutableDictionary alloc] init];
    _qiArr = [[NSMutableArray alloc] init];
    _zonArr = [NSMutableArray arrayWithCapacity:0];
    _floorArr = [NSMutableArray array];
    _oneArr = [NSMutableArray array];
    _twoArr = [NSMutableArray array];
    _threeArr = [NSMutableArray array];
    _oModelArr = [NSMutableArray array];
    _dataArr = [NSMutableArray array];
    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 108, B_WIDTH, B_HEIGHT-108) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
    
     [self setupMenuQiArr:_qiArr];
    

    
  
}
-(NSMutableArray *)oModelArr{
    if (_oModelArr) {
        return _oModelArr;
    }
    
    _oModelArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _oModelArr;
}

-(NSMutableArray *)changArr
{
    if (_changArr) {
        return _changArr;
    }
    
    _changArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _changArr;
}

-(NSMutableArray *)lastArr
{
    if (_lastArr) {
        return _lastArr;
    }
    
    _lastArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _lastArr;
}

-(void)setupMenuQiArr:(NSMutableArray *)qiArr//这一个方法在点击第一单元的时候出发不？
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    RKTagsMenuView *tagMenuView = [[RKTagsMenuView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width, 44)];
    tagMenuView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tagMenuView];
    [tagMenuView setTagsArray:qiArr];
    
    //  NSLog(@"bbbb-----%@",_unitStr);
    [_manager setUpCeoYouHuiPiZhuShouHouseWithProjectID:_projectID withUnit:_unitStr];
    
//    NSLog(@"------%@-------",_projectID);
//      NSLog(@"----222222--%@-------",_unitStr);
    [self.tabView reloadData];
    [tagMenuView setDidSelectTagBlock:^(NSInteger clickedRow) {
        
        //这个是点击第一单元,然后发的请求
        if(_changArr.count!=0  ){
            [_changArr removeAllObjects];
            [self.floorArr removeAllObjects];
            [self.dataArr removeAllObjects];
            [self.oModelArr removeAllObjects];
            
            [self.tabView reloadData];
          [_manager setUpCeoYouHuiPiZhuShouHouseWithProjectID:_projectID withUnit:qiArr[clickedRow]];
            
        }
        
        
    }];
    
    
}

- (void)houseBtnClick:(NSDictionary *)dic
{
    NSLog(@"--------dic----%@",dic);
    NSString *aaID = [dic objectForKey:@"B_TAG"];
    
    NSLog(@"--------a--%@",aaID);
    ZhuShouThreeVC *zhuThVC = [[ZhuShouThreeVC alloc] init];
    zhuThVC.allID = aaID ;
    NSLog(@"------------zhuThVC.allID-%@",aaID);
    [self.navigationController pushViewController:zhuThVC animated:NO];

    
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _floorArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static  NSString *ID = @"Cell";
    ZhuTwoCell *cell = [[ZhuTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    if ([cell isKindOfClass:[ZhuTwoCell class]])
    {
        if (_oModelArr.count != 0 ) {
            
            DataModel2 *mode = [_oModelArr objectAtIndex:indexPath.row];
            
            cell.oneLab.text = _floorArr[indexPath.row];
            
            houseID = mode.room_id;
            _changArr = self.houModel.data[indexPath.row].data;//
            NSMutableArray *stateArr = [NSMutableArray new];
            
            for (int i =0; i<_changArr.count; i++) {
                [stateArr addObject:[_changArr[i] is_sale]];
            }
            
            NSLog(@"stateArr===%@",stateArr);
            [(ZhuTwoCell *)cell setupCustomTableViewCellWIthBtnArr:_changArr withStatne:stateArr withShangLab:nil model:self.houModel];
            
        }
        
    }
    
    cell.delegate = self;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = BgColor;

    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
    headerView.backgroundColor = OverColor;
    [self.view addSubview:headerView];
    UILabel *louLab1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 20)];
    louLab1.textColor = [UIColor whiteColor];
    louLab1.text = @"楼层";
    louLab1.font = [UIFont systemFontOfSize:14];
    louLab1.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:louLab1];
    
    UILabel *lineLa1 = [[UILabel alloc] initWithFrame:CGRectMake(62, 0, 2, 40)];
    lineLa1.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:lineLa1];
    
    UILabel *fangLab1 = [[UILabel alloc] initWithFrame:CGRectMake(63, 10, B_WIDTH-63, 20)];
    fangLab1.textColor = [UIColor whiteColor];
    fangLab1.text = @"房源";
    fangLab1.font = [UIFont systemFontOfSize:14];
    fangLab1.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:fangLab1];
    
    return headerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这个是每一个楼层有几个房间
    NSInteger number = 1;
    // NSLog(@"change.count===%ld",_changArr.count);
    
    
    _changArr = self.houModel.data[indexPath.row].data;
    
    if (self.houModel.data[indexPath.row].data.count %3 == 0)
    {
        number =(_changArr.count)/3;
    }
    else
    {
        if (_changArr.count /3.0f < 1)
        {
            number = 1;
        }
        else
        {
            number = _changArr.count/3 + 1;
        }
    }
    
    
    
    
    return number *42.f;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *busnessFootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
    busnessFootView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:busnessFootView];
    
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 1)];
    linLab.backgroundColor = [UIColor lightGrayColor];
    [busnessFootView addSubview:linLab];
    UIButton *busnessAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //agreeBtn.frame = CGRectMake(20, 5, 80, 30);
    busnessAgreeBtn.layer.cornerRadius = 5;
    busnessAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [busnessAgreeBtn setTitle:@"同意执行" forState:0];
    [busnessAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [busnessAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [busnessAgreeBtn addTarget:self action:@selector(busnessAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [busnessFootView addSubview:busnessAgreeBtn];
    [busnessAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.equalTo(busnessFootView.mas_centerX).offset(-10);
        make.top.equalTo(busnessFootView.mas_top).offset(5);
    }];
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    unAgreeBtn.layer.cornerRadius = 5;
    unAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [unAgreeBtn setTitle:@"驳回修改" forState:0];
    [unAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [unAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [unAgreeBtn addTarget:self action:@selector(busnessUnAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [busnessFootView addSubview:unAgreeBtn];
    [unAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.left.equalTo(busnessFootView.mas_centerX).offset(10);
        make.top.equalTo(busnessFootView.mas_top).offset(5);
    }];
    return busnessFootView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_shenPiStr isEqualToString:@"1"])
    {
        return 40;
    }
    else
    {
        return 0;
    }
}



#pragma mark-----底部的按钮点击事件

-(void)busnessAgreeBtn:(UIButton *)btn
{
    
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"同意执行?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager setUpCeoShenPiYouHuiAgreeWithType:@"1" withProjectID:_projectID withUID:IDA];
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
    

    
}
-(void)busnessUnAgreeBtn:(UIButton *)btn
{
    
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"驳回修改?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            [_manager setUPCeoYouHUIPiUnAgreeWithType:@"1" WithID:_projectID withUid:IDA];
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
    
   
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
